package com.google.code.timeeditapplication.view.implementation;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.code.timeeditapplication.R;
import com.google.code.timeeditapplication.controller.implementation.ScheduleController;
import com.google.code.timeeditapplication.controller.implementation.ScheduleTools;
import com.google.code.timeeditapplication.model.implementation.Schedule;

/**
 * Describes the Week View
 * 
 * @author Jennifer
 */
public class WeekViewActivity extends Activity {

	/**
	 * sets the current view to week view
	 * @param savedInstanceState
	 */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.weekview);
		addSchedules();

	}

	@SuppressWarnings("unused")
	private final int textSize = 12;

	// Adding schedules to the weekview
	private void addSchedules() {
		final ArrayList<Schedule> list = ScheduleController.getInstance()
				.getWeekSchedule();

		final Date date = ScheduleController.getInstance().getActiveDate();
		final GregorianCalendar cal = new GregorianCalendar();
		cal.set(date.getYear() + 1900, date.getMonth(), date.getDate());

		final TextView weekNumber = (TextView) findViewById(R.id.weekviewweeklabel);
		weekNumber.setText("V." + cal.get(Calendar.WEEK_OF_YEAR));

		final TextView dayLabel1 = (TextView) findViewById(R.id.weekviewmondaylabel);
		final TextView dayLabel2 = (TextView) findViewById(R.id.weekviewtuesdaylabel);
		final TextView dayLabel3 = (TextView) findViewById(R.id.weekviewwednesdaylabel);
		final TextView dayLabel4 = (TextView) findViewById(R.id.weekviewthursdaylabel);
		final TextView dayLabel5 = (TextView) findViewById(R.id.weekviewfridaylabel);

		// Setting the date in the week views labels.
		final int currentDay = cal.get(Calendar.DAY_OF_WEEK);
		int m = 0, tu = 0, w = 0, th = 0, f = 0;

		switch (currentDay) {
		case 1:
			m = -7;
			tu = -6;
			w = -5;
			th = -4;
			f = -3;
		case 2:
			m = 1;
			tu = 2;
			w = 3;
			th = 4;
			f = 5;
		case 3:
			m = 0;
			tu = 1;
			w = 2;
			th = 3;
			f = 4;
		case 4:
			m = -1;
			tu = 0;
			w = 1;
			th = 2;
			f = 3;
		case 5:
			m = -2;
			tu = -1;
			w = 0;
			th = 1;
			f = 2;
		case 6:
			m = -3;
			tu = -2;
			w = -1;
			th = 0;
			f = 1;
		case 7:
			m = -4;
			tu = -3;
			w = -2;
			th = -1;
			f = 0;

		}
		// Rolling back/forth the set amount of days
		final Calendar cal1 = (Calendar) cal.clone();
		cal1.add(Calendar.DATE, m);
		final Calendar cal2 = (Calendar) cal.clone();
		cal2.add(Calendar.DATE, tu);
		final Calendar cal3 = (Calendar) cal.clone();
		cal3.add(Calendar.DATE, w);
		final Calendar cal4 = (Calendar) cal.clone();
		cal4.add(Calendar.DATE, th);
		final Calendar cal5 = (Calendar) cal.clone();
		cal5.add(Calendar.DATE, f);

		dayLabel1.setText(cal1.get(Calendar.DATE) + "/"
				+ (cal.get(Calendar.MONTH) + 1));
		dayLabel2.setText(cal2.get(Calendar.DATE) + "/"
				+ (cal.get(Calendar.MONTH) + 1));
		dayLabel3.setText(cal3.get(Calendar.DATE) + "/"
				+ (cal.get(Calendar.MONTH) + 1));
		dayLabel4.setText(cal4.get(Calendar.DATE) + "/"
				+ (cal.get(Calendar.MONTH) + 1));
		dayLabel5.setText(cal5.get(Calendar.DATE) + "/"
				+ (cal.get(Calendar.MONTH) + 1));

		final CustomView mView = ((CustomView) findViewById(R.id.WeekViewMonday));
		mView.addScheduleList(list.get(0).getItems(), 10);

		final CustomView tuView = ((CustomView) findViewById(R.id.WeekViewTuesday));
		tuView.addScheduleList(list.get(1).getItems(), 10);

		final CustomView wView = ((CustomView) findViewById(R.id.WeekViewWednesday));
		wView.addScheduleList(list.get(2).getItems(), 10);

		final CustomView thmView = ((CustomView) findViewById(R.id.WeekViewThursday));
		thmView.addScheduleList(list.get(3).getItems(), 10);

		final CustomView fView = ((CustomView) findViewById(R.id.WeekViewFriday));
		fView.addScheduleList(list.get(4).getItems(), 10);
	}

	/**
	 * Creates the menu in the week view
	 * 
	 * @param: Menu menu
	 */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		super.onCreateOptionsMenu(menu);

		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.week_view_menu, menu);
		return true;

	}

	/**
	 * Listens to the menu buttons
	 * 
	 * @param: MenuItem item
	 */
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
		case R.id.day_weekview:
			final Intent dayViewIntent = new Intent(this, DayViewActivity.class);
			startActivity(dayViewIntent);
			return true;
		case R.id.month_weekview:
			final Intent monthViewIntent = new Intent(this,
					MonthViewActivity.class);
			startActivity(monthViewIntent);
			return true;
		case R.id.save_as_my_schedule_weekview:
			ScheduleTools.saveAsMySchedule("My Schedule");
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}
}
